// pages/integral/integral.js
const app = getApp();
var utils = require("../../../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    allintegral: 0, // 我的积分
        //为空提示
    isNone: false,
    loading: true, //true显示， flase隐藏
    nomore: false, //没有更多提示
    detail: [], //积分详情容器
    detailPageSize: 10, //积分详情分页数据大小
    detailPage: 0,  //积分详情当前页
    recordPageSize: 10, //兑换记录分页数据大小
    recordPage: 0,  //兑换记录当前页
    record: [], //兑换记录容器
    activeIndex: 0,
    tab: ['积分明细', '兑换记录'],
    isSign: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.init();
  },

  init(cd){
    wx.showLoading({ title: '加载中...' });
    wx.request({
      url: app.baseURL + 'Integral&a=index',
      data: {
        user_id: app.globalData.userId
      },
      success: (res) => {
        if (res.statusCode == 200) {
          this.setData({
            info: res.data,
            detail: [],
            record: [],
            detailPage: 0,
            recordPage: 0,
          });
          wx.hideLoading();
          if (this.data.activeIndex == 1){
            this.integralRecord();
          }else{
            this.integralDetail();
          }
          typeof cb == 'function' && cb();
        }
      }
    });
  },
  //积分明细---兑换记录
  tabClick(e) {
    if (this.data.activeIndex != 0 &&e.currentTarget.id == 0) {
      this.setData({
        detail: [],
        detailPage: 0
      })
      this.integralDetail();
    }
    if (this.data.activeIndex != 1 &&e.currentTarget.id == 1) {
      this.setData({
        record: [],
        recordPage: 0
      })
      this.integralRecord();
    }
    this.setData({
      activeIndex: e.currentTarget.id
    });
  },

  //上拉加载
  onReachBottom: function () {
    if (this.data.activeIndex == 0){
      this.data.nomore || this.integralDetail();  //上拉加载
    }
    if (this.data.activeIndex == 1) {
      this.data.nomore || this.integralRecord();  //上拉加载
    }
  },

  //点击签到
  clicksign(){
    wx.showLoading({ title: "加载中..." });
    wx.request({
      url: app.baseURL + 'Integral&a=sign',
      data: {
        user_id: app.globalData.userId
      },
      success: (res) => {
        if (res.statusCode == 200) {
          this.setData({
            isSign: true,
            signData: res.data,
            'info.is_sign': "1",
          });
          this.init();
          wx.hideLoading();
        }
      }
    });
  },
  hideSign(){
    this.setData({
      isSign: false
    })
  },

  //获取积分明细
  integralDetail(){
    wx.showLoading({ title: "加载中..." });
    this.setData({
      loading: true,
      detailPage: this.data.detailPage + 1
    })
    wx.request({
      url: app.baseURL + 'Integral&a=integral_detail',
      data: {
        user_id: app.globalData.userId,
        pagesize: this.data.detailPageSize,  //每页数据量
        page: this.data.detailPage,  //页数
      },
      success: (res) => {
        if (res.statusCode == 200) {
          const data = this.data;
          res.data.detail.forEach((item, index) => {
            // 显示剩余时间
            item.addTime = utils.formatTime(item.add_time * 1000);
          });
          //更新数组内容
          data.detail = [...data.detail, ...res.data.detail];
          this.setData({
            detail: data.detail,
            isNone: data.detail.length > 0 ? false : true,  //判断是否为空
            nomore: res.data.detail.length < this.data.detailPageSize,  //判断是否已经加载到了最后一页
            loading: false,
            emptyTip: {
              font: "您还没有积分记录哦~",
              imgUrl: "https://rybc.0791jr.com/WeChat/images/empty-detail.png"
            },
          });
        };
        wx.hideLoading();
      }
    })
  },

  integralRecord(){
    wx.showLoading({ title: "加载中..." });
    this.setData({
      loading: true,
      recordPage: this.data.recordPage + 1
    })
    wx.request({
      url: app.baseURL + 'Integral&a=record',
      data: {
        user_id: app.globalData.userId,
        size: this.data.recordPageSize,  //每页数据量
        page: this.data.recordPage,  //页数
      },
      success: (res) => {
        if (res.statusCode == 200) {
          const data = this.data;
          //更新数组内容
          data.record = [...data.record, ...res.data.info];
          this.setData({
            record: data.record,
            isNone: data.record.length > 0 ? false : true,  //判断是否为空
            nomore: res.data.info.length < this.data.recordPageSize,  //判断是否已经加载到了最后一页
            loading: false,
            emptyTip: {
              font: "您暂时还没有兑换记录哦~",
              imgUrl: "https://rybc.0791jr.com/WeChat/images/empty-info.png"
            },
          })
          wx.hideLoading();
        }
      }
    })
  },
  //下拉刷新
  onPullDownRefresh() {
    this.init(wx.stopPullDownRefresh());
  },
})